package com.cskaoyan.service.wxcatalog;

import com.cskaoyan.bean.MallCategory;
import com.cskaoyan.bean.MallCategoryExample;
import com.cskaoyan.mapper.MallCategoryMapper;
import com.cskaoyan.model.vo.wxcategory.WxCategoryIndexVo;
import com.cskaoyan.model.vo.wxcategory.WxCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: mjm
 * @Date: 2021/12/2
 * @Time: 15:51
 * @Description:
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Autowired
    MallCategoryMapper mallCategoryMapper;


    @Override
    public WxCategoryVo catalogCurrent(Integer id) {
        MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(id);

        MallCategoryExample example = new MallCategoryExample();
        example.createCriteria().andPidEqualTo(mallCategory.getId()).andDeletedEqualTo(false).andLevelEqualTo("L2");

        //根据pid查询
        List<MallCategory> mallCategories = mallCategoryMapper.selectByExample(example);

        WxCategoryVo wxCategoryVo = new WxCategoryVo();
        wxCategoryVo.setCurrentCategory(mallCategory);
        wxCategoryVo.setCurrentSubCategory(mallCategories);
        return wxCategoryVo;
    }

    @Override
    public WxCategoryIndexVo catalogIndex() {
        WxCategoryIndexVo wxCategoryIndexVo = new WxCategoryIndexVo();

        //categoryList
        MallCategoryExample example = new MallCategoryExample();
        example.createCriteria().andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MallCategory> mallCategories = mallCategoryMapper.selectByExample(example);
        wxCategoryIndexVo.setCategoryList(mallCategories);

        //获取currentCategory，默认为第一个
        //TODO
        MallCategory currentCategory = new MallCategory();
        if(mallCategories != null){
            currentCategory = mallCategoryMapper.selectByPrimaryKey(mallCategories.get(0).getId());
            wxCategoryIndexVo.setCurrentCategory(currentCategory);
        }

        //获取currentSubCategory
        List<MallCategory> currentSubCategory = new ArrayList<>();
        if(currentCategory != null){
            MallCategoryExample example1 = new MallCategoryExample();
            example1.createCriteria().andPidEqualTo(currentCategory.getPid()).andDeletedEqualTo(false).andLevelEqualTo("L2");
            currentSubCategory = mallCategoryMapper.selectByExample(example);
            wxCategoryIndexVo.setCurrentSubCategory(currentSubCategory);
        }
        return wxCategoryIndexVo;
    }
}
